﻿using ElectronicShoppingMall.Service.Models.Clinet;
using ElectronicShoppingMall.Service.Services.Client;
using ElectronicShoppingMall.Web.Front.DTOS;
using Extension;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Results;

namespace ElectronicShoppingMall.Web.Front.Controllers
{
    public class OrderController : ApiController
    {
        private readonly OrderService _service;
        public OrderController()
        {
            _service = new OrderService();
        }
        [HttpPut]
        [Authorize]
        public String ConfirmOrder([FromBody]ConfirmOrderDTO confrim)
        {
            try
            {
                ConfirmOrderModel confirmModel = new ConfirmOrderModel
                {
                    Consignee = confrim.Consignee,
                    DetailedAddress = confrim.DetailedAddress,
                    PhoneNumber = confrim.PhoneNumber,
                    UserId = confrim.UserId
                };
                confirmModel.Goods = new List<CartGoodsInfoModel>();
                foreach (var goodsInfo in confrim.Goods)
                {
                    confirmModel.Goods.Add(new CartGoodsInfoModel
                    {
                        GoodsId = goodsInfo.GoodsId,
                        Number = goodsInfo.Number
                    });
                }
                _service.ConfirmOrder(confirmModel);
                return "OK";
            }
            catch
            {
                return null;
            }
        }
        [HttpGet]
        [Authorize]
        public List<OrderDTO> GetUserUnpaidOrders()
        {
            string token = Request.Headers.Authorization.ToString();
            var userId = ExtensionMethods.GetClientUserObject(token).UserId;
            try
            {
                List<OrderDTO> result = new List<OrderDTO>();
                var orders = _service.GetUserUnpaidOrders(userId);
                foreach (var order in orders)
                {
                    OrderDTO temp = new OrderDTO
                    {
                        Consignee = order.Consignee,
                        Count = order.Count,
                        CreateTime = order.CreateTime.ToShortTimeString(),
                        GoodsName = order.GoodsName,
                        PhoneNumber = order.PhoneNumber,
                        Total = order.Price * order.Count,
                        DeliveryAddress = order.DeliveryAddress,
                        OrderId = order.OrderId,
                        State = order.State
                    };
                    result.Add(temp);
                }
                return result;
            }
            catch
            {
                return null;
            }

        }
        [HttpPost]
        [Authorize]
        public String PaymentOrders()
        {
            try
            {
                string token = Request.Headers.Authorization.ToString();
                var userId = ExtensionMethods.GetClientUserObject(token).UserId;
                return _service.PaymentOrders(userId);
            }
            catch
            {
                return null;
            }
        }
        [HttpGet]
        [Authorize]
        public List<OrderDTO> GetUserOrders([FromUri]int state)
        {
            try
            {
                string token = Request.Headers.Authorization.ToString();
                var userId = ExtensionMethods.GetClientUserObject(token).UserId;
                var orders = _service.GetUserOrders(userId, state);
                List<OrderDTO> result = new List<OrderDTO>();
                foreach (var order in orders)
                {
                    OrderDTO temp = new OrderDTO
                    {
                        Consignee = order.Consignee,
                        Count = order.Count,
                        CreateTime = order.CreateTime.ToShortTimeString(),
                        GoodsName = order.GoodsName,
                        PhoneNumber = order.PhoneNumber,
                        Total = order.Price * order.Count,
                        DeliveryAddress = order.DeliveryAddress,
                        OrderId = order.OrderId,
                        State = order.State
                    };
                    if (order.ApplicationTime != null)
                    {
                        temp.ApplicationTime = order.ApplicationTime.ToString();
                    }
                    result.Add(temp);
                }
                return result;
            }
            catch
            {
                return null;
            }
        }
        [HttpPatch]
        [Authorize]
        public List<OrderDTO> OrderOperation([FromBody] OrderOperationDTO operation)
        {
            try
            {
                string token = Request.Headers.Authorization.ToString();
                var userId = ExtensionMethods.GetClientUserObject(token).UserId;
                switch (operation.OperationType)
                {
                    case "cancel":
                        _service.CancelOrder(userId, operation.Order.OrderId);
                        break;
                    case "applyForRefund":
                        _service.ApplyForRefund(userId, operation.Order.OrderId, operation.Order.ReasonForApplication);
                        break;
                    case "cancelRefund":
                        _service.CancelRefund(userId, operation.Order.OrderId);
                        break;
                    case "confirmReceipt":
                        _service.ConfirmReceipt(userId, operation.Order.OrderId);
                        break;
                    case "applicationReturns":
                        _service.ApplicationReturns(userId, operation.Order.OrderId, operation.Order.ReasonForApplication);
                        break;
                    case "cancelReturns":
                        _service.CancelReturns(userId, operation.Order.OrderId);
                        break;
                    case "deleteOrder":
                        _service.DeleteOrder(userId, operation.Order.OrderId);
                        break;
                    default:
                        throw new Exception("不合法参数");
                }
                return GetUserOrders(10);
            }
            catch
            {
                return null;
            }
        }
    }
}
